package fr.sportcom.sportcom;

import java.util.ArrayList;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;

import fr.sportcom.sportcom.model.CustomHttpClient;
import fr.sportcom.sportcom.model.User;
import android.app.Application;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;

public class SportComApplication extends Application {
    private User loggedUser = null;
    private ArrayList<Integer> adsCycle = new ArrayList<Integer>();
    private Drawable imgPub = null;

    public User getLoggedUser() {
        return loggedUser;
    }

    public void setLoggedUser(User user) {
        this.loggedUser = user;
    }

    public void signOff() {
        this.loggedUser = null;
    }
    
    public int nextPubId() {
    	int temp = getPubId();
    	adsCycle.add(adsCycle.remove(0));
    	InitDrwAdsTask task = new InitDrwAdsTask();
    	task.execute(getPubId());
    	return temp;
    }
    
    public int getPubId() {
    	return adsCycle.get(0);
    }
    
    public Drawable getDrawAds() {
    	return imgPub;
    }
    
    public void initAds() {
    	if(adsCycle.size() == 0) {
	    	InitAdsListTask task = new InitAdsListTask();
	    	task.execute();
    	}
    }
    
    
    private class InitAdsListTask extends AsyncTask<Void, Void, Boolean> {
        protected Boolean doInBackground(Void... params) {
        	CustomHttpClient request = new CustomHttpClient();
			JSONObject jObj = null;
			ArrayList<NameValuePair> param = new ArrayList<NameValuePair>();
			try {
				jObj = request.postData("getAdsCycle", param);
				if(jObj.getInt("back") != 0) {
					return false;
				}

				adsCycle.clear();
				JSONArray list = jObj.getJSONArray("list");
				for(int i=0; i<list.length(); i++) {
					adsCycle.add(list.getInt(i));
				}

			} catch (Exception e) {
				return false;
			}

			return true;
        }

        protected void onPostExecute(final Boolean success) {
        	InitDrwAdsTask task = new InitDrwAdsTask();
        	task.execute(getPubId());
        	
        	ScheduledExecutorService scheduleTaskExecutor = Executors.newScheduledThreadPool(900);
        	// This schedule a runnable task every 2 minutes
        	scheduleTaskExecutor.scheduleAtFixedRate(new Runnable() {
        	  public void run() {
        		  nextPubId();
        	  }
        	}, 30, 60, TimeUnit.SECONDS);
        }
    }
    
    private class InitDrwAdsTask extends AsyncTask<Integer, Void, Boolean> {
        protected Boolean doInBackground(Integer... params) {
        	String url = CustomHttpClient.HTTP_BASE_URL + "img/" + params[0] + ".png";
			try {
				imgPub = Drawable.createFromStream(((java.io.InputStream) new java.net.URL(url).getContent()), "src");
			} catch (Exception e) {
				return false;
			}
			return true;
        }

        protected void onPostExecute(final Boolean success) {}
    }
}
